﻿using ERP.Domain.Products;
using Product.API.ISerivces.DTOS;
using Product.API.ISerivces.Process;
using Repository.communal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Product.API.Serivces.Process
{
    /// <summary>
    /// 工序服务实现类
    /// </summary>
    public class ProcessSerivce : IProcessSerivce
    {
        /// <summary>
        /// 工序仓储
        /// </summary>
        private readonly IBaseRepository<ERP.Domain.Products.Process> processRepository;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="processRepository">工序仓储</param>
        public ProcessSerivce(IBaseRepository<ERP.Domain.Products.Process> processRepository)
        {
            this.processRepository = processRepository;
        }

        /// <summary>
        /// 批量添加工序
        /// </summary>
        /// <param name="entity">工序数组</param>
        /// <param name="productProcessNumber">工艺流程编号</param>
        /// <returns>返回受影响函数</returns>
        public Task<int> BatchProcess(List<BatchProcessDTO> entity, Guid productRouteNumber)
        {
            var data = entity.Select(item => new ERP.Domain.Products.Process
            {
                SerialNumber = item.SerialNumber,
                ProcessName = item.ProcessName,
                NextProcess = item.NextProcess,
                Relationship = item.Relationship,
                IsCriticalProcess = item.IsCriticalProcess,
                PreparationTime = item.PreparationTime,
                WaitingTime = item.WaitingTime,
                Remarks = item.Remarks,
                ProcessRouteNumber = productRouteNumber,
                IsDelete = item.IsDelete
            }).ToList();

            var result = processRepository.BatchAdd(data);
            return result;
        }

        /// <summary>
        /// 工序添加
        /// </summary>
        /// <param name="process">工序信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> CreateProcess(ERP.Domain.Products.Process process)
        {
            var result = await processRepository.AddAsync(process);
            return result;
        }

        /// <summary>
        /// 工序删除
        /// </summary>
        /// <param name="Id">工序主键</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteProcess(int Id)
        {
            var data = await processRepository.GetModelAsync(Id);
            data.IsDelete = true;
            int result = await processRepository.Update(data);
            return result;
        }

        /// <summary>
        /// 工序修改
        /// </summary>
        /// <param name="process">工序信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateProcess(ERP.Domain.Products.Process process)
        {
            var result = await processRepository.Update(process);
            return result;
        }

        /// <summary>
        /// 批量修改工序
        /// </summary>
        /// <param name="entity">工序数组</param>
        /// <param name="processRouteNumber">工艺流程编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> BatchUpdateProcess(List<BatchUpdateProcessDTO> entity, Guid processRouteNumber)
        {
            var data = entity.Select(item => new ERP.Domain.Products.Process
            {
                Id = item.Id,
                ProcessNumber = item.ProcessNumber,
                SerialNumber = item.SerialNumber,
                ProcessName = item.ProcessName,
                NextProcess = item.NextProcess,
                Relationship = item.Relationship,
                IsCriticalProcess = item.IsCriticalProcess,
                PreparationTime = item.PreparationTime,
                WaitingTime = item.WaitingTime,
                Remarks = item.Remarks,
                ProcessRouteNumber = processRouteNumber,
                IsDelete = item.IsDelete
            }).ToList();

            var result = await processRepository.BatchUpdate(data);
            return result;
        }

    }
}
